import 'package:flutter/material.dart';
import 'package:interview/components/page/page_load_overlay.dart';
import 'package:interview/components/page/page_nav_bar.dart';
import 'package:interview/components/status_bar_warpper.dart';
import 'package:interview/theme/app_colors.dart';
import 'package:webview_flutter/webview_flutter.dart'; // 导入 WebView 插件

class WebPage extends StatefulWidget {
  final String title;
  final String url;

  const WebPage({super.key, required this.title, required this.url});

  @override
  State<WebPage> createState() => _WebPageState();
}

class _WebPageState extends State<WebPage> {
  late WebViewController _controller; // WebView 控制器
  bool _isLoading = true; // 控制加载状态

  @override
  void initState() {
    super.initState();

    // 初始化 WebView 插件
    _controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted) // 启用 JavaScript
      ..loadRequest(Uri.parse(widget.url)); // 加载页面

    // 确保插件已初始化
    WidgetsBinding.instance.addPostFrameCallback((_) {
      // 在页面加载开始时显示进度条
      PageLoadOverlay.show(
        context: context,
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).extension<AppColors>()!;
  
    return StatusBarWrapper(
      statusBarColor: theme.subjectPure!,
      statusBarIconBrightness: Theme.of(context).brightness == Brightness.dark? Brightness.light: Brightness.dark,
      child: Scaffold(
        appBar: null,
        body: SafeArea(
          child: Stack(
            children: [
              if (_isLoading)
                Center(child: SizedBox.shrink()), // 显示加载指示器
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16), 
                    child: PageNavBar(
                      theme: theme,
                      showTitle: true,
                      title: widget.title,
                      showRight: false,
                    ),
                  ),
                  Expanded(
                    child: WebViewWidget(
                      controller: _controller
                        ..setNavigationDelegate(
                          NavigationDelegate(
                            onPageStarted: (String url) {
                              setState(() {
                                _isLoading = true;
                              });
                            },
                            onPageFinished: (String url) {
                              setState(() {
                                _isLoading = false;
                              });
                              // 隐藏加载框
                              PageLoadOverlay.hide();
                            },
                            onProgress: (int progress) {
                              // 更新进度条
                            },
                          ),
                        ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
